"""
分析不同批次大小和滑动步长组合的效果
"""
from config import Config
from data_loader import DataLoader

def analyze_batch_configurations():
    """分析不同批次大小和滑动步长组合"""
    config = Config()
    
    print("=== 不同批次大小和滑动步长组合分析 ===")
    print(f"总文件数: {config.TOTAL_DATA}")
    print(f"序列长度: {config.SEQ_LEN}")
    
    # 测试不同的批次大小
    batch_sizes = [5, 10, 16, 20, 32, 50]
    sliding_steps = [1, 2, 3, 5, 10]
    
    print(f"\n{'批次大小':<8} {'滑动步长':<8} {'序列数':<8} {'批次数':<8} {'训练样本':<10} {'数据增强':<8}")
    print("-" * 70)
    
    best_configs = []
    
    for batch_size in batch_sizes:
        for sliding_step in sliding_steps:
            # 临时修改配置
            temp_config = Config()
            temp_config.BATCH_SIZE = batch_size
            
            try:
                data_loader = DataLoader(temp_config, sliding_step=sliding_step)
                total_sequences = data_loader.get_total_sequences()
                max_batches = data_loader.get_max_batches()
                
                if max_batches >= 3:  # 至少需要3个批次才有意义
                    # 计算训练样本数（假设20%用于测试）
                    test_batches = max(1, int(max_batches * 0.2))
                    train_batches = max_batches - test_batches
                    train_samples = train_batches * batch_size
                    
                    # 数据增强倍数
                    original_sequences = config.TOTAL_DATA // config.SEQ_LEN
                    enhancement = total_sequences / original_sequences
                    
                    print(f"{batch_size:<8} {sliding_step:<8} {total_sequences:<8} {max_batches:<8} {train_samples:<10} {enhancement:<8.1f}x")
                    
                    # 收集好的配置
                    if train_samples >= 100:  # 至少100个训练样本
                        best_configs.append({
                            'batch_size': batch_size,
                            'sliding_step': sliding_step,
                            'total_batches': max_batches,
                            'train_batches': train_batches,
                            'test_batches': test_batches,
                            'train_samples': train_samples,
                            'enhancement': enhancement
                        })
                        
            except Exception as e:
                print(f"{batch_size:<8} {sliding_step:<8} 错误: {str(e)[:20]}")
    
    # 推荐最佳配置
    print(f"\n=== 推荐配置 ===")
    
    if best_configs:
        # 按训练样本数排序
        best_configs.sort(key=lambda x: x['train_samples'], reverse=True)
        
        print(f"按训练样本数排序的前5个配置:")
        print(f"{'排名':<4} {'批次大小':<8} {'滑动步长':<8} {'训练批次':<8} {'训练样本':<10} {'增强倍数':<8}")
        print("-" * 60)
        
        for i, config in enumerate(best_configs[:5]):
            print(f"{i+1:<4} {config['batch_size']:<8} {config['sliding_step']:<8} "
                  f"{config['train_batches']:<8} {config['train_samples']:<10} {config['enhancement']:<8.1f}x")
        
        # 推荐配置
        best_config = best_configs[0]
        print(f"\n最佳推荐配置:")
        print(f"BATCH_SIZE = {best_config['batch_size']}")
        print(f"SLIDING_STEP = {best_config['sliding_step']}")
        print(f"MAX_TRAIN_BATCHES = {best_config['train_batches']}")
        print(f"TEST_BATCHES = {best_config['test_batches']}")
        print(f"总训练样本数: {best_config['train_samples']}")
        print(f"总测试样本数: {best_config['test_batches'] * best_config['batch_size']}")
        
        # 平衡配置（中等数据量，较好的批次大小）
        balanced_configs = [c for c in best_configs if 16 <= c['batch_size'] <= 32 and c['train_samples'] >= 200]
        if balanced_configs:
            balanced_config = balanced_configs[0]
            print(f"\n平衡推荐配置（推荐）:")
            print(f"BATCH_SIZE = {balanced_config['batch_size']}")
            print(f"SLIDING_STEP = {balanced_config['sliding_step']}")
            print(f"MAX_TRAIN_BATCHES = {balanced_config['train_batches']}")
            print(f"TEST_BATCHES = {balanced_config['test_batches']}")
            print(f"总训练样本数: {balanced_config['train_samples']}")
            print(f"总测试样本数: {balanced_config['test_batches'] * balanced_config['batch_size']}")
    
    return best_configs

if __name__ == "__main__":
    configs = analyze_batch_configurations() 